home *** CD-ROM | disk | FTP | other *** search
- Path: newsfeed.direct.ca!usenet
- From: etoivane@direct.ca (Ed Toivanen)
- Newsgroups: comp.lang.c
- Subject: Warning 300 lines of code, and I don't know where the problem is!
- Date: 17 Feb 1996 16:44:41 GMT
- Organization: Your Organization
- Message-ID: <4g50lp$ejf@aphex.direct.ca>
- NNTP-Posting-Host: 204.174.243.117
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=US-ASCII
- X-Newsreader: WinVN 0.99.6
-
- I don't know where the problem lies in my code. It prints out garbage to the
- screen the first time option 5 is selected, but works fine from that point
- on. Also, when I change the code by adding a printf() in a "safe" spot, such
- as the debug lines, it outputs garbage again! It is a small, flat database of
- students and their grades. Only options 0, 1, 4, 5 have been implemented.0=
- quit, 1= add a record, 4= print a record, 5= print the entire database.
- Can you help?
-
- Thanks, Ed
-
- assign2.h--------------------------------------
- /*
- Assign2.h
-
- Ed Toivanen
- Assign2
- Comp 3425(Wed)
- Feb 5, 1996
- */
- #define NAME_LEN 40
- #define MAX_STUDENTS 100
-
- typedef enum tagBool {FALSE, TRUE} bool;
-
- typedef enum tagAction {QUIT, ADD_REC, DELETE_REC, EDIT_REC, PRINT_REC,
- PRINT_DB, FIND_REC} ACTION;
-
- typedef enum tagProgram{SCIENCE, ART} PROGRAM;
-
- typedef struct tagScience_mark {
- int math, physics, compsci; /* Range: 0..100 */
- }SCIENCE_MARK;
-
- typedef struct tagArt_mark {
- int acting, dancing; /* Range: 0..100 */
- }ART_MARK;
-
- typedef union tagMark {
- SCIENCE_MARK scientist;
- ART_MARK artist;
- }MARK;
-
- typedef struct tagStudent_record {
- int id; /* Range: 1..99, Unique */
- char name[NAME_LEN + 1]; /* Non-unique */
- PROGRAM major;
- MARK marks;
- } STUDENT_RECORD;
-
- typedef struct tagIndex {
- long fpos;
- int id;
- char name[NAME_LEN + 1];
- } INDEX;
-
- ACTION queryUser(void);
- bool doAction(FILE*, STUDENT_RECORD*, ACTION, INDEX[]);
- bool initList(char*, FILE*);
- bool getInfo(STUDENT_RECORD*);
- bool addRec(FILE*, STUDENT_RECORD*);
- int writeRec(FILE*, STUDENT_RECORD*);
- bool deleteRec(FILE*, STUDENT_RECORD*);
- bool findRec(FILE*, STUDENT_RECORD*);
- bool printList(FILE*);
- bool printRec(FILE*, STUDENT_RECORD*, INDEX*, int);
- bool updateIndex(FILE*, STUDENT_RECORD*, INDEX[]);
- bool printDb(FILE*, STUDENT_RECORD*, INDEX*);
- int getFileSize(FILE*);
-
-
-
- assign2.c----------------------------------------------
- /*
- assign2.c
-
- Ed Toivanen
- Assign 2
- Comp 3425(Fri)
- Feb 5, 1996
- */
-
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "assign2.h"
-
- int main(void){
- char indexList[13 + 1];
- FILE file, * fp;
- STUDENT_RECORD rec,* pRec;
- INDEX index[MAX_STUDENTS];
- ACTION action;
-
- fp = &file;
- pRec = &rec;
-
- printf("Enter database file to open\n");
- gets(indexList);
- strcat(indexList, ".dat");
- /*fflush(stdout);*/
-
- fp = fopen(/*indexList*/"student.dat", "a+b");
- if(fp == NULL){
- fprintf(stderr, "%s NULL fp\n", indexList);
- return(1);
- }
-
- while(action = queryUser()){
- /*fflush(stdin);*/
- doAction(fp, pRec, action, index);
- updateIndex(fp, pRec, index);
- }
- fclose(fp);
- printf("End of program\n");
- return(0);
- }/*end main()*/
-
- bool initList(char* list, FILE* fp){
- if(!(fp = fopen(list, "wb"))){
- fprintf(stderr, "Error opening %s\n", list);
- fclose(fp);
- return(FALSE);
- }
- else
- return(TRUE);
- }/*end initList()*/
-
- bool getInfo(STUDENT_RECORD* rec){
- char buf[100];
- int num;
-
- printf("Student id:\t");
- fgets(buf, sizeof(buf), stdin);
- printf("\n");
- rec->id=atoi(buf);
-
- printf("Student's last name first:\t");
- fgets(buf, sizeof(buf), stdin);
- /*buf[strlen(buf)] = '\0';
- printf("\n");*/
- strcpy(rec->name, buf);
-
- printf("Science(press 1) or Art(press 2)?:\t");
- /*while(!((num=atoi(fgets(buf, sizeof(buf), stdin)) > 0) && (num < 3)))
- {}*/
- num = atoi(fgets(buf, sizeof(buf), stdin));
- if(num == 1)
- rec->major = SCIENCE;
- else
- rec->major = ART;
-
- printf("Grades\n");
- switch(rec->major){
- case SCIENCE:
- printf("\tMath:\t");
- fgets(buf, sizeof(buf), stdin);
- printf("\n");
- rec->marks.scientist.math = atoi(buf);
-
- printf("\tPhysics:\t");
- fgets(buf, sizeof(buf), stdin);
- printf("\n");
- rec->marks.scientist.physics = atoi(buf);
-
- printf("\tCompsci:\t");
- fgets(buf, sizeof(buf), stdin);
- printf("\n");
- rec->marks.scientist.compsci = atoi(buf);
- break;
-
- case ART:
- printf("\tActing:\t");
- fgets(buf, sizeof(buf), stdin);
- printf("\n");
- rec->marks.artist.acting = atoi(buf);
-
- printf("\tDancing:\t");
- fgets(buf, sizeof(buf), stdin);
- printf("\n");
- rec->marks.artist.dancing = atoi(buf);
- break;
-
- default:
- break;
- }/*end switch*/
- return(TRUE);
- }/*End getInfo()*/
-
- int writeRec(FILE* fp, STUDENT_RECORD* rec){
- int i;
-
- fseek(fp, 0, SEEK_END);
- if(i=fwrite(rec, sizeof(*rec), 1, fp)!=NULL)
- ;
- else
- fprintf(stderr,"Error writing to file. %d records written\n", i);
- return(i);
- }/*end writeRec()*/
-
- ACTION queryUser(void){
- char buf[3];
-
- printf("Please enter selection number\n");
- printf("\tQuit\t\t0\n\n");
- printf("\tAdd a record\t 1 \n\n");
- printf("\tDelete a record\t 2 \n\n");
- printf("\tEdit a record\t 3 \n\n");
- printf("\tPrint a record\t 4 \n\n");
- printf("\tPrint out the entire database\t 5 \n\n");
- printf("\t?:");
-
- fgets(buf, sizeof(buf), stdin);
- printf("\n");
- return((ACTION)atoi(buf));
- }/*end queryUser()*/
-
- bool doAction(FILE* fp, STUDENT_RECORD* rec, ACTION action, INDEX index[]){
- char sBuf[10];
- int iRecNum;
- bool bResult;
- switch(action){
- case QUIT:
- bResult = FALSE;
- break;
-
- case ADD_REC:
- addRec(fp, rec);
- bResult = TRUE;
- break;
-
- case DELETE_REC:
- bResult = deleteRec(fp, rec) ? TRUE : FALSE;
- break;
-
- case EDIT_REC:
- addRec(fp, rec);
- bResult = TRUE;
- break;
-
- case PRINT_REC:
- printf("\tRecord number?:\t");
- iRecNum = atoi(fgets(sBuf, sizeof(sBuf), stdin));
- /*bResult =*/ printRec(fp, rec, index, iRecNum) /*? TRUE : FALSE*/;
- bResult=TRUE;
- break;
-
- case PRINT_DB:
- /*bResult =*/ printDb(fp, rec, index) /*? TRUE : FALSE*/;
- bResult=TRUE;
- break;
-
- default:
- bResult = FALSE;
- break;
- }/*end switch*/
- printf("\n");
- return(bResult);
- }/*end doAction()*/
-
- int getFileSize(FILE* fp){
- int iSavedPos, iFileSize;
-
- iSavedPos = ftell(fp);
- fseek(fp, 0L, SEEK_END);
- iFileSize = ftell(fp);
- fseek(fp, iSavedPos, SEEK_SET);
- return(iFileSize);
- }
-
- bool updateIndex(FILE* fp, STUDENT_RECORD* rec, INDEX index[]){
- int i=0, fileSize, recSize;
- FILE *file;
-
- fileSize = getFileSize(fp);
- recSize = sizeof(*rec);
-
- while(!feof(fp)){
- fseek(fp, i * recSize, SEEK_SET);
- index[i].fpos = ftell(fp);
- fread(rec, 1, recSize, fp);
- index[i].id = rec->id;
- strcpy(index[i].name, rec->name);
- /*debug
- printf("ftell(fp)=%d\t rec->id=%d \t rec->name=%s\n", ftell(fp),
- rec->id, rec->name);
- printf("fpos=%d\t id=%d\t name=%s\n", index[i].fpos, index[i].id,
- index[i].name);
- end debug*/
- i++;
- }
-
- return(TRUE);
- }/*end updateIndex()*/
-
- bool printRec(FILE* fp, STUDENT_RECORD* rec, INDEX index[], int i){
- fseek(fp, index[i].fpos, SEEK_SET);
- fread(rec, 1, sizeof(*rec), fp);
- printf("%d\n", rec->id);
- printf("%s", rec->name);
- switch(rec->major){
- case SCIENCE:
- printf("Science major\n");
- printf("%d\n", rec->marks.scientist.math);
- printf("%d\n", rec->marks.scientist.physics);
- printf("%d\n", rec->marks.scientist.compsci);
- break;
-
- case ART:
- printf("Art major\n");
- printf("%d\n", rec->marks.artist.acting);
- printf("%d\n", rec->marks.artist.dancing);
- break;
-
- default:
- break;
- }/*end switch*/
- printf("\n");
- return(TRUE);
- }/*end printRec()*/
-
- bool printDb(FILE* fp, STUDENT_RECORD* rec, INDEX* index){
- int i, iFileSize, iRecSize;
-
- iFileSize = getFileSize(fp);
- iRecSize = sizeof(*rec);
- fseek(fp, 0L, SEEK_SET);
- for(i=0; i * iRecSize < iFileSize; i++){
- printRec(fp, rec, index, i);
- }
- return(TRUE);
- }/*end printDb()*/
-
- bool findRec(FILE* fp, STUDENT_RECORD* rec){
- return(TRUE);
- }/*end rindRec()*/
-
- bool addRec(FILE * fp, STUDENT_RECORD * rec){
- getInfo(rec);
- writeRec(fp, rec);
- return(TRUE);
- }
-
- bool deleteRec(FILE* fp, STUDENT_RECORD* rec ){
- return(TRUE);
- }/*end deleteRec()*/
-
-
-